﻿using System;
using System.Collections.Generic;
using Dapper;
using zhangmenren.Db;
using zhangmenren.Models;

namespace zhangmenren.Repository
{
    public class CensusRepository: BaseRepository
    {
        public int Add(CensusEntity entity)
        {
            string sql = @"INSERT INTO [Census] 
                            (WorkId
                            ,UserId
                            ,Type
                            ,AddDT)
                        VALUES
                            (@WorkId
                            ,@UserId
                            ,@Type
                            ,GETDATE())";
            return Execute(connection => connection.Execute(sql,entity));
        }

        public int Del(int id)
        {
            string sql = @"DELETE FROM Census WHERE Id = @Id";
            return Execute(connection => connection.Execute(sql, new { Id = id }));
        }

        public IEnumerable<CensusEntity> FindByUserId(int id)
        {
            string sql = @"SELECT Id,WorkId,UserId,Type FROM Census WHERE UserId=@UserId";
            return Execute(connection => connection.Query<CensusEntity>(sql,new {UserId=id }));  //Items = items.Skip((pageIndex - 1) * pageSize).Take(pageSize)
        }
        public IEnumerable<CensusEntity> FindByWorkId(int id)
        {
            string sql = @"SELECT Id,WorkId,UserId,Type FROM Census WHERE WorkId=@WorkId";
            return Execute(connection => connection.Query<CensusEntity>(sql, new { WorkId = id }));  //Items = items.Skip((pageIndex - 1) * pageSize).Take(pageSize)
        }
    }
}
